﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataProcessing;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Media.Media3D;
using System.Windows;
namespace Gene.Render
{
    /// <summary>
    /// 计算窗口的放缩，移动的类
    /// </summary>
    public class CWindowViewer
    {
        /// <summary>
        /// 通过当前缩放比例，和所观察窗口的index来确定观察窗口值域范围
        /// </summary>
        /// <param name="zoomRatio"></param>
        /// <param name="windowIndex"></param>
        /// <returns></returns>
        public static ValueRange GetWindow(int zoomRatio,double windowIndex)
        {
            int windowSize = (int)((ChromePairData.AllWindowMax.Size % zoomRatio == 0) ? (ChromePairData.AllWindowMax.Size / zoomRatio) : (ChromePairData.AllWindowMax.Size / zoomRatio + 1));
         
            ValueRange observingWindow = new ValueRange();
            observingWindow.Start = (uint)(ChromePairData.AllWindowMax.Start + windowIndex * (ChromePairData.AllWindowMax.Size - windowSize) );
            observingWindow.End = Math.Min((uint)(observingWindow.Start + windowSize - 1), ChromePairData.AllWindowMax.End);
            return observingWindow;
        }
        /// <summary>
        /// 根据当前用户关注点和缩放比例来计算观察窗口的值域范围
        /// 用来实现根据用户点击来放大的功能
        /// 应该利用CoordinateDetector里面的方法来进行坐标转换，获得用户实际关心的点
        /// </summary>
        /// <param name="zoomRatio"></param>
        /// <param name="focus"></param>
        /// <returns></returns>
        public static ValueRange GetWindow(ref double windowIndex,Point3D focus, ValueRange currentWindow, double xCoordinateStart, double xCoordinateEnd, bool isAmplifying)
        {
            ValueRange observingWindow = new ValueRange();
            uint windowSize;
            if (isAmplifying)
            {
                windowSize = currentWindow.Size / 2;
                double quarterPos = xCoordinateStart + (xCoordinateEnd - xCoordinateStart) / 4;
                double threeQuarterPos = xCoordinateEnd - (xCoordinateEnd - xCoordinateStart) / 4;

                if (focus.X < quarterPos)//取当前左半部分
                {
                    observingWindow.Start = currentWindow.Start;
                    observingWindow.End = currentWindow.Start + windowSize - 1;
                }
                else if (focus.X > threeQuarterPos)
                {
                    observingWindow.End = currentWindow.End;
                    observingWindow.Start = currentWindow.End - windowSize + 1;
                }
                else
                {
                    uint currentPosInWindow = (uint)(currentWindow.Start + (currentWindow.End - currentWindow.Start) * ((focus.X - xCoordinateStart) / (xCoordinateEnd - xCoordinateStart)));
                    observingWindow.Start = currentPosInWindow - windowSize / 2;
                    observingWindow.End = observingWindow.Start + windowSize - 1;
                }
            }
            else
            {
                windowSize = currentWindow.Size * 2;
                if (windowSize > ChromePairData.AllWindowMax.Size)
                    return null;
                observingWindow.Start = Math.Max(ChromePairData.AllWindowMax.Start, currentWindow.Start - windowSize / 4);
                observingWindow.End = observingWindow.Start + windowSize - 1;
                if (observingWindow.End > ChromePairData.AllWindowMax.End)
                {
                    observingWindow.End = Math.Min(ChromePairData.AllWindowMax.End, observingWindow.End);
                    observingWindow.Start = observingWindow.End - windowSize + 1;
                }
            }
            windowIndex = (double)(observingWindow.Start - ChromePairData.AllWindowMax.Start) / (ChromePairData.AllWindowMax.Size - windowSize);
            return observingWindow;
        }

            
    }
}
